package com.greatchn.authorization_server.web.srv;

import com.greatchn.authorization_server.web.dao.IOauthRoleFunctionConnectionDao;
import com.greatchn.authorization_server.web.dao.IOauthRoleGroupInfoDao;
import com.greatchn.authorization_server.web.po.OauthRoleFunctionConnection;
import com.greatchn.authorization_server.web.po.OauthRoleGroupInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @OauthRoleFunctionConnectionSrv: OauthRoleFunctionConnection srv
 * @author: ZBoHang
 * @time: 2023/2/28 9:24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OauthRoleFunctionConnectionSrv {

    @Resource
    private IOauthRoleFunctionConnectionDao iOauthRoleFunctionConnectionDao;
    @Resource
    private IOauthRoleGroupInfoDao iOauthRoleGroupInfoDao;

    /**
     * 通过功能id 获取可用角色
     */
    public List<OauthRoleGroupInfo> getAllRoleIdByFunctionIds(List<Integer> functionIdList) {

        List<OauthRoleFunctionConnection> roleFunctionConnList = this.iOauthRoleFunctionConnectionDao.findByFunctionIdIn(functionIdList);

        List<Integer> roleGroupIdList =
                roleFunctionConnList.stream().map(OauthRoleFunctionConnection::getRoleGroupId).collect(Collectors.toList());

        return this.iOauthRoleGroupInfoDao.findByRoleGroupIdIn(roleGroupIdList);
    }
}
